package com.CmJava.servlet.executionChain.handlerMethodMapping;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class HandlerMethod {
    private Class<?> beanType;
    private Method method;
    private MethodParameter[] parameters;

    public HandlerMethod(Class<?> beanType, Method method, MethodParameter[] parameters) {
        this.beanType = beanType;
        this.method = method;
        this.parameters = parameters;
    }

    public Class<?> getBeanType() {
        return beanType;
    }

    public void setBeanType(Class<?> beanType) {
        this.beanType = beanType;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public MethodParameter[] getParameters() {
        return parameters;
    }

    public void setParameters(MethodParameter[] parameters) {
        this.parameters = parameters;
    }

    public Object invoke(Object[] args) {
        Object result = null;
        try {
            Object bean = beanType.newInstance();
            if (method.getParameterTypes().length>0){
                //不支持传参哦，我不会在这里用反编译获取参数名
                throw new RuntimeException("不支持传参哦，我不会在这里用反编译获取参数名");
            }else {
                result = method.invoke(bean);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return result;
    }
}
